<?php
namespace App\Repositories;

//引入hash

use App\Response\Response;
use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use App\Services\Tools\RedisService;

class NobilityBuyRepository
{
    /*
     *购买爵位列表
     * @param $nobility_id
     * @param $keyword
     * @param $page
     * @param $perPage
     * @return bool
     * @throws BasicException
     * */
    public function buyNobilityList($nobility_id, $keyword, $page, $perPage)
    {
        $list = app('model_nobility_buy')
            ->select('id', 'nobility_id', 'user_id', 'money', 'add_time', 'expiration_time')
            ->where('status', 2)
            ->with(['user_data:user_id,user_code,nickname,head_picture', 'nobility:id,head_picture as picture,name as nobility_name']);
        if ($nobility_id) {
            $list = $list->where('nobility_id', $nobility_id);
        }
        if ($keyword) {
            $list = $list->where(function ($query) use ($keyword) {
                $query->whereHas('user_data', function ($query) use ($keyword) {
                    $query->where('user_code', 'like', "%{$keyword}%")->orWhere('nickname', 'like', "%{$keyword}%");
                });
            });
        }
        $list = $list->orderBy('add_time', 'desc')->paginate($perPage, ['*'], '', $page);

        return $list;
    }

    //购买爵位
    public function buyNobility($user_id, $nobility_id = 0, $days = 30)
    {
        if ($nobility_id <= 0) {
            throw new BasicException(0, '请选择要赠送的爵位');
        }
        $nobility = RedisService::getNobilityConfig($nobility_id);
        if (!$nobility) {
            throw new BasicException(0, '爵位不存在');
        }
        $nobility_id = $nobility['id'];
        //获取用户当前爵位
        $user_nobility = app('model_user_nobility')->where(['user_id' => $user_id])->first();

        $now_time = time();
        $expire_time = $now_time + $days * 86400;//有效期30天
        DB::beginTransaction();
        try {
            $data['nobility_id'] = $nobility_id;
            $data['money'] = $nobility['price'];
            $data['type'] = 3;
            $data['coin'] = 0;
            $data['add_type'] = 3;
            $data['add_time'] = $now_time;
            $data['user_id'] = $user_id;
            $data['order_sn'] = createOrderSn();
            $data['expiration_time'] = $expire_time;//有效期30天
            $data['status'] = 2;
            $data['pay_time'] = $now_time;
            $data['trade_no'] = 0;
            $data['type'] = 0;
            $id = app('model_nobility_buy')->insertGetId($data);
            if (!$id) {
                throw new BasicException(0, '生成记录失败');
            }

            //获取当前的爵位
            if (!$user_nobility) {
                app('model_user_nobility')->insert([
                    'user_id'      => $user_id,
                    'nobility_id'  => $nobility_id,
                    'expired_time' => $expire_time,
                    'state'        => 1,
                ]);
            } else {
                app('model_user_nobility')->where(['user_id' => $user_id])->update([
                    'nobility_id'  => $nobility_id,
                    'expired_time' => $expire_time,
                    'state'        => 1,
                ]);
            }

            switch ($nobility_id) {
                case 1:
                    $mall_product_id = 1;
                    break;
                case 2:
                    $mall_product_id = 2;
                    break;
                case 3:
                    $mall_product_id = 3;
                    break;
                case 4:
                    $mall_product_id = 4;
                    break;
                case 5:
                    $mall_product_id = 5;
                    break;
                case 6:
                    $mall_product_id = 6;
                    break;
                default:
                    $mall_product_id = 0;
            }
            if ($mall_product_id > 0) {
                app('model_mall_orders')->give($mall_product_id, $user_id, $days);
            }

            //更新用户爵位等级
            app('model_user')->where(['user_id' => $user_id])->update(['nobility' => $nobility_id]);

            DB::commit();

            RedisService::setUserNobility($user_id, $nobility_id);
            RedisService::clearUserInfo($user_id);
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException($e->getCode(), $e->getMessage());
        }
    }
}
